// 向量标注相关API接口
import { get, post, put, del } from '../utils/request'

/**
 * 获取指定颜色的向量维度数据
 * @param {number} colorId - 颜色ID
 * @returns {Promise<Object>} 向量维度数据
 */
export const getColorDimensions = async colorId => {
  try {
    const response = await get(`/color/${colorId}/dimensions`)
    return response.data
  } catch (error) {
    console.error('获取颜色维度数据失败:', error)
    throw error
  }
}

/**
 * 保存指定颜色的向量维度数据
 * @param {number} colorId - 颜色ID
 * @param {Object} dimensionsData - 维度数据
 * @returns {Promise<Object>} 保存结果
 */
export const saveColorDimensions = async (colorId, dimensionsData) => {
  try {
    const response = await post(`/color/${colorId}/dimensions`, dimensionsData)
    return response.data
  } catch (error) {
    console.error('保存颜色维度数据失败:', error)
    throw error
  }
}

/**
 * 获取字典数据
 * @param {string} type - 字典类型 (clothingTypes, colors, patterns, materials)
 * @returns {Promise<Array>} 字典数据
 */
export const getDictionaries = async (type = null) => {
  try {
    const params = type ? { type } : {}
    const response = await get('/dictionaries', params)
    return response.data
  } catch (error) {
    console.error('获取字典数据失败:', error)
    throw error
  }
}

/**
 * 获取特定类型的字典
 * @param {string} type - 字典类型
 * @returns {Promise<Array>} 字典数据
 */
export const getDictionaryByType = async type => {
  try {
    const response = await get(`/dictionaries/${type}`)
    return response.data
  } catch (error) {
    console.error(`获取${type}字典数据失败:`, error)
    throw error
  }
}

/**
 * 创建字典条目
 * @param {string} type - 字典类型
 * @param {Object} itemData - 条目数据
 * @returns {Promise<Object>} 创建的条目
 */
export const createDictionaryItem = async (type, itemData) => {
  try {
    const response = await post(`/dictionaries/${type}`, itemData)
    return response.data
  } catch (error) {
    console.error(`创建${type}字典条目失败:`, error)
    throw error
  }
}

/**
 * 更新字典条目
 * @param {string} type - 字典类型
 * @param {string} itemId - 条目ID
 * @param {Object} itemData - 更新的条目数据
 * @returns {Promise<Object>} 更新后的条目
 */
export const updateDictionaryItem = async (type, itemId, itemData) => {
  try {
    const response = await put(`/dictionaries/${type}/${itemId}`, itemData)
    return response.data
  } catch (error) {
    console.error(`更新${type}字典条目失败:`, error)
    throw error
  }
}

/**
 * 删除字典条目
 * @param {string} type - 字典类型
 * @param {string} itemId - 条目ID
 * @returns {Promise<boolean>} 删除结果
 */
export const deleteDictionaryItem = async (type, itemId) => {
  try {
    const response = await del(`/dictionaries/${type}/${itemId}`)
    return response.code === 200
  } catch (error) {
    console.error(`删除${type}字典条目失败:`, error)
    throw error
  }
}
